var API = require('API');
module.exports = ['$scope','$timeout',function ($scope,$timeout) {

    $scope.user_add_parm = {};
    $scope.user_add_parm.sex = 0;
    $scope.user_revise_parm = [];
    $scope.revise_user = true;
    $scope.revise_user_title = true;

    
    $scope.user_view_shops = [];
    $scope.view_shop_remove =[];
    $scope.all_view_shops = [];
    $scope.all_view_shops_del =[];

    $scope.forms = {
        data:'',
        del:'del',
        user:{}
    };
    $scope.refresh = function(){
        API.user.brief(function (data) {
            $scope.$apply(function(){
                $scope.forms.data = data;    
            });
        });  
    };
    $scope.refresh();
    $scope.delete = function(item){
        console.log(item);
        if (confirm('确定删除吗？') == true) {
            API.user.del(item.uid, function () {
                alert('删除成功！');
                $scope.refresh();
            });
        }
    };





    $scope.$watch('forms.user',function(data){

        console.log('sss',$scope);

        if (data.uid) {
            $scope.user_revise_parm = data;
            API.user.get(data.uid, function (u) {
                $scope.$apply(function () {
                    $scope.user_view_shops = (u._view_shops || "").split(',');

                    $scope.all_view_shops_del = Array.prototype.slice.call($scope.all_view_shops, 0)
                    .filter(function (brand) {
                        return $scope.user_view_shops.indexOf(brand) == -1;
                    });

                    // $scope.add_user = false;
                    $scope.revise_user = true;
                    // $scope.add_user_title = false;
                    $scope.revise_user_title = true;
                    $scope.user_revise_parm = u;
                    $scope.user_add_parm = {};
                })
            });
        }
    });


    $scope.add_view_shops = function(){

        $scope.view_shop_add.forEach(function(selected){
            var rm_idx = $scope.all_view_shops_del.indexOf(selected);
            if(selected != -1){
                $scope.all_view_shops_del.splice(rm_idx,1);
            }
            $scope.user_view_shops.unshift(selected);
        })
        $scope.view_shop_add =[];

    }
    $scope.remove_view_shops = function(){
        $scope.view_shop_remove.forEach(function(selected){

            var rm_idx = $scope.user_view_shops.indexOf(selected);
            if(selected != -1){
                $scope.user_view_shops.splice(rm_idx,1);    
            }
            $scope.all_view_shops_del.unshift(selected);
            
        })
        $scope.view_shop_remove = [];
    };

    //获取所有门店信息
    function shop_all() {
        API.shop.all(function (data) {
            $scope.$apply(function () {
                $scope.shop_all_data = data;

                $scope.all_view_shops = data.map(function(i){return i.name});

            })
        })
    };
    shop_all();


    //用户信息提交修改
    $scope.user_revise_submit = function () {
        $scope.user_revise_parm._view_shops = $scope.user_view_shops.join(',');
        console.log($scope.user_revise_parm);
        API.user.upd($scope.user_revise_parm, function () {
            $scope.$apply(function () {
                $scope.user_revise_parm = {};
                $scope.user_add_parm = {};
                $scope.refresh();
            });
        })
    };


    //增加用户
    $scope.user_add_submit = function () {
        API.user.add($scope.user_add_parm, function (data) {
            
            $scope.user_add_parm = {};
            $scope.user_add_open = false;
            $scope.refresh();
        });
    };

    //删除用户
    $scope.user_del_submit = function (user) {
        if (confirm('确定删除吗？') == true) {
            API.user.del(user.uid, function () {
                alert('删除成功！');
                $scope.refresh();
            });
        }
    };

    //刷新用户列表
    $scope.syn_users_to_rbac = function() {
      API.rbac.reload(function(){
          alert('同步成功');
          $scope.refresh();
      });
    }
}];